package com.day1012.morning;

import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class MainText3 {
    public static void main(String[] args) throws Exception {
        Map<Integer,String> map = new HashMap<>();
        map.put(1,"public");
        map.put(2,"private");
        map.put(3,"protected");
        Teacher teacher = new Teacher();
        Class clazz = teacher.getClass();
        String c_name = clazz.getName().replace("com.day1012.","");//  类名
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(map.get(field.getModifiers()) + " " + field.getType().getName() + " " +  field.getName());
        }
        String wuc = map.get(1) + " " + c_name + "(){\n}\n";//  无参构造
        String name0h = fields[0].getType().getName().replace("java.lang.","");//  去掉String前面的Java.lang.
        //  有参构造
        String youc = map.get(1) + " " + c_name + "(" + name0h + " " + fields[0].getName() + "," + name0h + " " + fields[1].getName() + "," + fields[2].getType() + " " + fields[2].getName() + "){\n}\n";
        // get 方法
        String[] gets = new String[3];
        String[] sets = new String[3];
        for (int i = 0; i < fields.length; i++) {
            gets[i] = map.get(1) + " " + fields[i].getType().getName().replace("java.lang.","") + " get" + fields[i].getName().substring(0,1).toUpperCase() + fields[i].getName().substring(1) + "() {\n    " + "return " + fields[i].getName() + ";\n}\n";
            sets[i] = map.get(1) + " void set" + fields[i].getName().substring(0,1).toUpperCase() + fields[i].getName().substring(1) + "(" + fields[i].getType().getName().replace("java.lang.","") + " " + fields[i].getName() + ") {\n   this." + fields[i].getName() + " = " + fields[i].getName() + ";\n}\n";
        }
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(wuc);
        stringBuffer.append(youc);
        stringBuffer.append(gets[0]);
        stringBuffer.append(sets[0]);
        stringBuffer.append(gets[1]);
        stringBuffer.append(sets[1]);
        stringBuffer.append(gets[2]);
        stringBuffer.append(sets[2]);
        File file = new File("D:\\IDEA\\lzy_java\\src\\main\\java\\com\\day1012\\Teacher.java");

        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);  //  读
        String str = null;
        StringBuffer readerJava = new StringBuffer();
        while ((str = bufferedReader.readLine()) != null){
            readerJava.append(str);
        }
        int index = readerJava.lastIndexOf(";");
        readerJava.insert(index+1,stringBuffer);
        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);//  写
        bufferedWriter.write(readerJava.toString());
        bufferedReader.close();
        fileReader.close();
        bufferedWriter.flush();
        bufferedWriter.close();
        fileWriter.close();


    }
}
